using System;
using System.Collections.Generic;
using System.Linq;

namespace Kotonoha.Server.UI.Comparators {
    public class ComparatorFactory<T> where T:class {
        private ComparatorGenerator generator;

        public ComparatorFactory() {
            generator = new ComparatorGenerator(typeof (T));
        }

        public MasterComparator<T> CreateComparersFor(bool reverse, params string[] fields) {
            return new MasterComparator<T>(fields.Select(f => Create(f)).ToList(), reverse);
        }

        public IComparer<T> Create(string propertyChain) {
            Type t = generator.CreateComparator(propertyChain);
            object o = t.GetConstructor(Type.EmptyTypes).Invoke(null);
            return (IComparer<T>) o;
        }
    }
}